package com.gilight.base.util;

import cn.hutool.core.io.resource.ClassPathResource;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * 李珂
 */
public class DbSearchBuilder {
    private static volatile DbSearcher dbSearcher;
    private static String  path = "ip2region/ip2region.db";
    private static String name = "ip2region.db";

    private DbSearchBuilder(){}

    public static DbSearcher getInstance(){
        if(dbSearcher == null){
            synchronized (DbSearchBuilder.class){
                if(dbSearcher == null){
                    DbConfig config = null;
                    try {
                        config = new DbConfig();
                        File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
                         dbSearcher = new DbSearcher(config, file.getPath());
                         dbSearcher.memorySearch("10.10.10.10");
                        return dbSearcher;
                    } catch (DbMakerConfigException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }else{
            return dbSearcher;
        }
        return null;
    }
}
